package com.zhss.eshop.order.price.promotion.gift;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zhss.eshop.commodity.domain.dto.CommodityGoodsSkuDTO;
import com.zhss.eshop.commodity.service.CommodityService;
import com.zhss.eshop.order.domain.dto.OrderItemDTO;
import com.zhss.eshop.order.price.promotion.AbstractGiftPromotionActivityCalculator;
import com.zhss.eshop.order.price.promotion.PromotionActivityCalculator;
import com.zhss.eshop.order.price.promotion.PromotionActivityResult;
import com.zhss.eshop.promotion.domain.dto.PromotionActivityDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 买赠促销类型的促销活动的价格计算组件
 * @author suyuan
 *
 */
@Component
public class DirectGiftPromotionActivityCalculator
		extends AbstractGiftPromotionActivityCalculator
		implements PromotionActivityCalculator {

	/**
	 * 商品中心接口
	 */
	@Autowired
	private CommodityService commodityService;

	@Override
	public PromotionActivityResult calculate(OrderItemDTO item,
											 PromotionActivityDTO promotionActivity) {
		JSONObject rule = JSONObject.parseObject(promotionActivity.getRule());
		JSONArray giftGoodsSkuIds = rule.getJSONArray("giftGoodsSkuIds");

		PromotionActivityResult result = new PromotionActivityResult();

		for(int i = 0; i < giftGoodsSkuIds.size(); i++) {
			Long goodsSkuId = giftGoodsSkuIds.getLong(i);
			CommodityGoodsSkuDTO goodsSku = commodityService.getGoodsSkuById(goodsSkuId);
			result.getOrderItems().add(createOrderItem(goodsSku));
		}

		return result;
	}

}
